
<template>
<el-card>
    <my-search v-model="pgData">
        <el-form-item slot="time" label="状态">
            <my-select :data="shbjList" :props="{label:'label',value:'value'}" v-model="pgData.shbj" style="width: 100%;"></my-select>
        </el-form-item>
        <el-form-item slot="title" label="地区">
            <city-cascader 
                v-model="pgData.xzqycode"  
                :change-on-select="false" 
                clearable 
                city-only 
                placeholder="请选择地区">
            </city-cascader>
        </el-form-item>

        <el-form-item  slot="title" >
            <el-input  v-model="pgData.dwname" placeholder="合作单位名称" clearable></el-input>
        </el-form-item>

        <el-button slot="chaxunbtn" size="small" type="primary" icon="el-icon-search" @click="search">查询</el-button>

        <div slot="dr" class="title_btns">
            <el-button size="small" type="primary" icon="el-icon-plus" @click="adddw" v-show="['0','1'].indexOf($store.getters.user.parentcode) != '-1' || ['0','1'].indexOf(getQuery('parentcode')) != '-1' ">添加单位</el-button>
        </div>
    </my-search>

    <el-table :data="form.tableData" :height="tableHeight" border row-key="dwcode" default-expand-all :tree-props="{children: 'children', hasChildren: 'hasChildren'}" :cell-style="{padding: '5px'}">
        <el-table-column prop="dwcode" label="单位编码" sortable min-width="130"></el-table-column>
        <el-table-column prop="xzqyname" label="地区" min-width="100"></el-table-column>
        <el-table-column prop="dwname" label="合作单位名称" min-width="140"></el-table-column>
        <el-table-column prop="fzrname" label="负责人"  min-width="60"></el-table-column>
        <el-table-column prop="phone" label="联系电话"  min-width="100"></el-table-column>
        <el-table-column prop="weburl" label="推广链接" min-width="240">
             <template slot-scope="scope">
                <div class="web_url_area" v-if="scope.row.weburl">
                    <div class="url_area">
                        {{scope.row.weburl +'?partnerid=' + scope.row.dwcode }}
                    </div>
                    <div class="tag_area">
                        <el-tag size="small"  @click="handleCopy(scope.$index, scope.row)"  style="cursor:pointer">复制</el-tag> 
                    </div>
                </div>
            </template>
        </el-table-column>
        <el-table-column prop="addtime" label="创建时间" sortable min-width="120"></el-table-column>
        <el-table-column prop="shbj" label="状态"  min-width="70">
            <template slot-scope="scope">
                  <el-tag v-if="scope.row.shbj == 1" type="success" size="small">启用</el-tag>
                  <el-tag v-if="scope.row.shbj == 0" type="info" size="small">未启用</el-tag>
            </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="200">
            <template slot-scope="scope">
                <el-button type="text" icon="el-icon-document" @click="detaildw(scope.row)">详情</el-button>
                <el-button type="text" icon="el-icon-edit-outline" @click="editdw(scope.row)" v-if="scope.row.parentcode == getGetters('user').dwcode ||(scope.row.dwcode == getGetters('user').dwcode && getGetters('user').dwcode=='1')  ||scope.row.parentcode == getQuery('dwcode') || (scope.row.dwcode == getQuery('dwcode') && getQuery('dwcode') == '1') ">编辑</el-button>
                <el-button type="text" icon="el-icon-delete" @click="deldw(scope.row)" v-if="scope.row.parentcode == getGetters('user').dwcode || scope.row.parentcode == getQuery('dwcode')">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <!-- 添加/编辑-->
    <my-dialog
        :title="formTitle"
        :width="'1150px'"
        v-model="dialogAddVisible">
        <add ref="add" v-if="dialogAddVisible" @close="dialogAddVisible = false" :dwcode="dwcode"></add>
    </my-dialog>

</el-card>

</template>

<script>
import  add from './add.vue'


export default {
    props:[],
    components:{add},
    data: function(){
        return{
            //搜索条件
            pgData: {
                shbj:'',
                dwname:'',
                xzqycode:'',

            },
            form: {
                tableData: [],
            },
            
            shbjList:[
                {label:'启用',value:1},
                {label:'未启用',value:0}
            ],
            formTitle:'',
            dwcode:'',
            dialogAddVisible:false,
            tableHeight: window.innerHeight - 160
        }
    }, 
    computed:{
 
    },
    watch:{
        dialogAddVisible: function(n, o){
            if(n == false){
                this.getparList();
            }
        },
    },

    methods: {
        //搜索
        search:function(){
            this.getparList();
        },
        detaildw:function(row){
            if(this.getQuery('token')){
                this.goto({
                    path: './detail',
                    query: {
                        dwcode: row.dwcode,
                        token:this.getQuery('token')
                    }
                })
            }else{
                this.goto({
                    path: './detail',
                    query: {
                        dwcode: row.dwcode
                    }
                })
            }
        },

        adddw:function(){
            this.dialogAddVisible = true;
            this.formTitle = '添加单位';
            this.dwcode = '';
        },
        editdw:function(row){
            this.dialogAddVisible = true;
            this.formTitle = '编辑单位';
            this.dwcode = row.dwcode;
        },
        deldw:function(row){
            var that = this;
            ShowConfirm('是否删除当前单位' , 'warning', function() {
                that.$delete(that.$store.getters.partnerUrl + '/partner/'+row.dwcode,{},function(data){
                    ShowMsg('删除成功','success');
                    that.getparList();
                })
            }, function() {
                  ShowMsg('已取消删除操作','info');
            })
        },

        //获取列表
        getparList:function(){
            this.$get(this.$store.getters.partnerUrl + '/partner', {
                dwname:this.pgData.dwname,
                shbj:this.pgData.shbj,
                xzqycode:this.pgData.xzqycode
            }, 
            function(data) {
                this.form.tableData = data;
                this.tableHeight = window.innerHeight - 155
            })
        },

        //复制
        handleCopy:function(index,row){
            this.copyData = row.weburl + '?partnerid=' + row.dwcode
            this.copy(this.copyData);
        },
        copy:function(data){
            let url = data;
            let oInput = document.createElement('input');
            oInput.value = url;
            document.body.appendChild(oInput);
            oInput.select(); // 选择对象;
            document.execCommand("Copy"); // 执行浏览器复制命令
            ShowMsg('复制成功','success');
            oInput.remove();
        },

    },
    mounted() {
        this.getparList();
    }
}
</script>

<style scoped lang="scss">

    .title_btns{
        float: right;
    }

    .web_url_area{
        display: flex;
        justify-content: space-between; 
        align-items: center;
    }
    .tag_area{
        min-width: 4em;
        text-align: right;
    }
    ::v-deep .el-table th > .cell {
        text-align: center;
    }


</style>
